from django.conf import settings
from django.db import models


class Project(models.Model):
    STATUS_CHOICES = [
        ('ACTIVE', '进行中'),
        ('COMPLETED', '已完成'),
        ('ARCHIVED', '已归档'),
    ]
    
    name = models.CharField('项目名称', max_length=32)
    intro = models.CharField('项目简介', max_length=256, default="")
    url = models.CharField('项目地址', max_length=256, default="")
    status = models.CharField('项目状态', max_length=20, choices=STATUS_CHOICES, default='ACTIVE')
    pytest_config = models.TextField('pytest配置脚本', default="")
    users = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, related_name="projects_as_member")
    pms = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, on_delete=models.SET_NULL,
                            related_name="projects_as_pm")
    created_time = models.DateTimeField('创建时间', auto_now_add=True, null=True)
    updated_time = models.DateTimeField('更新时间', auto_now=True)
    
    class Meta:
        verbose_name = '项目'
        verbose_name_plural = '项目列表'
        
    def __str__(self):
        return self.name


class Environment(models.Model):
    """环境配置"""
    name = models.CharField('环境名称', max_length=32)
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    base_url = models.CharField('基础URL', max_length=255)
    variables = models.JSONField('环境变量', blank=True, default=dict)
    headers = models.JSONField('全局请求头', blank=True, default=dict)
    created_time = models.DateTimeField('创建时间', auto_now_add=True)
    updated_time = models.DateTimeField('更新时间', auto_now=True)
    
    class Meta:
        verbose_name = '环境配置'
        verbose_name_plural = '环境配置列表'
        
    def __str__(self):
        return f"{self.project.name} - {self.name}"
